'use strict';
const Client = require('ssh2').Client;
const Service = require('egg').Service;
class SSHService extends Service {
  /**
     * 创建SSH连接
     * @param {String} ip 远程服务ip地址
     * @param {Number} port ssh服务端口
     * @param {String} username 远程服务器用户名
     * @param {String} password 远程服务用户名密码
     * @return {Object} connection SSH连接句柄
     */
  createClient({ host, port, username, password }) {
    console.log('userinfo:', host, port, username, password);
    let self = this;
    const connection = new Client();
    const options = {host, port, username, password,...this.config.sshConfig};
    return new Promise((resolve, reject) => {
        connection.on('ready', function(err) {
            console.log('Client :: ready',err);
            resolve(connection);
        }).connect(options);
        connection.on('error', () => {
            resolve( false);
        });
    });
  }
}
module.exports = SSHService;